<?php

class quotePagesTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this -> addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
      new sfCommandOption('id', null, sfCommandOption::PARAMETER_REQUIRED, 'Author from which to start', '0'),
      // add your own options here
    ));

    $this -> namespace = 'authors';
    $this -> name = 'pages';
    $this -> briefDescription = 'Recalculate quote_pages column from author table';
    $this -> detailedDescription = <<<EOF
The [quotePages|INFO] task does things.
Call it with:

  [php symfony quotePages|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this -> configuration);
    $connection = $databaseManager -> getDatabase($options['connection']) -> getConnection();

    // add your code here
    $authors = AuthorQuery::create()
      ->filterById($options['id'], Criteria::GREATER_THAN)
      ->find();
    foreach ($authors as $author)
    {
      $quotes_count = QuoteQuery::create()->filterByAuthorId($author->getId())->count();
      $quote_pages = ceil($quotes_count / 10);
      $author->setQuotePages(($quote_pages));
      $author->save();
      $this->log($author->getId().'. '.$author->getName().' now has '.$quote_pages.' quote pages');
    }
  }

}
